package com.gnerv.cms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.cms.dto.*;
import com.gnerv.cms.entity.Org;
import com.gnerv.cms.entity.OrgInfo;
import com.gnerv.cms.mapper.OrgInfoMapper;
import com.gnerv.cms.mapper.OrgMapper;
import com.gnerv.cms.service.IOrgService;
import com.gnerv.cms.utils.UUIDUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 基础平台-组织机构 service 实现类
 *
 * @author zhang
 * @date 2019/11/30
 **/
@Slf4j
@Service
public class OrgServiceImpl implements IOrgService {

    @Resource
    private OrgMapper orgMapper;

    @Resource
    private OrgInfoMapper orgInfoMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveOrg(OrgSaveDTO orgSaveDTO) {
        String ukId = orgSaveDTO.getUkId();
        if (StringUtils.isEmpty(ukId)) {
            createOrg(orgSaveDTO);
        } else {
            updateOrg(orgSaveDTO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean createOrg(OrgSaveDTO orgSaveDTO) {
        Org org = orgSaveDTO.convert();
        org.setUkId(UUIDUtils.getLower());
        // 添加组织机构信息
        orgMapper.createOrg(org);
        // 添加组织机构详情信息
        OrgInfoDTO orgInfo = orgSaveDTO.getOrgInfo();
        if (!StringUtils.isEmpty(orgInfo)) {
            OrgInfo info = orgInfo.convert();
            info.setOrgId(org.getUkId());
            orgInfoMapper.insert(info);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteOrg(String orgId) {
        // 删除组织机构详情，然后删除组织机构信息 和 与 组织机构绑定用户信息
        unBindOrgUser(orgId);
        orgInfoMapper.deleteById(orgId);
        orgMapper.deleteById(orgId);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateOrg(OrgSaveDTO orgSaveDTO) {
        OrgInfoDTO orgInfo = orgSaveDTO.getOrgInfo();
        if (orgInfo != null) {
            OrgInfo info = orgInfo.convert();
            info.setOrgId(orgSaveDTO.getUkId());
            info.setGmtModified(LocalDateTime.now());
            orgInfoMapper.updateById(info);
        }
        orgSaveDTO.convert().setGmtModified(LocalDateTime.now());
        orgMapper.updateById(orgSaveDTO.convert());
        return true;

    }

    @Override
    public OrgDTO selectOrg(String orgId) {
        return orgMapper.selectOrg(orgId);
    }

    @Override
    public List<OrgTreeDTO> listOrgTree(String parentId) {
        return orgMapper.listOrgTree(parentId);
    }

    @Override
    public Page<OrgDTO> listOrg(Integer current, Integer size, OrgSearchDTO orgSearchDTO) {
        Page<OrgDTO> page = new Page<>(current, size);
        return orgMapper.listOrg(page, orgSearchDTO);
    }

    @Override
    public boolean hasChildren(String orgId) {
        return orgMapper.hasChildren(orgId) > 0L;
    }

    @Override
    public boolean bindOrgUser(String orgId, List<String> userIds) {
        orgMapper.bindOrgUser(orgId, userIds);
        return true;
    }

    @Override
    public boolean bindOrgUser(String orgId, String userId) {
        List<String> userIds = Lists.newArrayList();
        userIds.add(userId);
        bindOrgUser(orgId, userIds);
        return true;
    }

    @Override
    public boolean unBindOrgUser(String orgId) {
        orgMapper.unBindOrgUser(orgId);
        return true;
    }

    @Override
    public boolean unBindUserOrg(String userId) {
        orgMapper.unBindUserOrg(userId);
        return true;
    }

    @Override
    public boolean updateOrgStatus(String orgId, Integer status) {
        orgMapper.updateOrgStatus(orgId, status);
        return true;
    }
}
